'--------------------------------------------------------'
' Description:
'   This script intends for printing pin's net name to its pad
'--------------------------------------------------------'
Option Explicit

' Add any type libraries to be used. 
Scripting.AddTypeLibrary("MGCPCB.ExpeditionPCBApplication")

CALL RoundViaCoord2Int

Function RoundViaCoord2Int()
    Dim pcbDoc
    Set pcbDoc = RetainActiveDoc()

	Call RoundVias(pcbDoc)
	Set pcbDoc = Nothing
End Function

Function RoundVias(pcbDoc)
	'get all selected vias:epcbSelectSelected 
	'doc.Vias(epcbSelectSelected)
    Dim viasColl
    Set viasColl = pcbDoc.Vias(epcbSelectSelected)
	
	Dim viaIter
	Dim offset2AddX
	Dim offset2AddY
	Dim targetVia
	
	For Each viaIter in viasColl
		If (viaIter.FixLock = epcbFixLockNone) Then
			On Error Resume Next
			set targetVia = viaIter
			offset2AddX = targetVia.PositionY - Int(targetVia.PositionY)
			offset2AddY = targetVia.PositionX - Int(targetVia.PositionX)
			Call targetVia.MoveRelative(-offset2AddX,-offset2AddY)
		End If
	Next
End Function

Function RetainActiveDoc()
' Get the application object 
    Dim pcbApp
    Set pcbApp = Application

    ' Get the active document 
    Dim pcbDoc
    Set pcbDoc = pcbApp.ActiveDocument

    ' License the document 
    ValidateServer(pcbDoc)

    Set RetainActiveDoc = pcbDoc
End Function

' Server validation function
Function ValidateServer(docObj)
  
  Dim keyInt
  Dim licenseTokenInt
  Dim licenseServerObj

  ' Ask Expedition’s document for the key
  keyInt = docObj.Validate(0)

  ' Get license server
  Set licenseServerObj = CreateObject("MGCPCBAutomationLicensing.Application")

  ' Ask the license server for the license token
  licenseTokenInt = licenseServerObj.GetToken(keyInt)

  ' Release license server
  Set licenseServerObj = nothing

  ' Turn off error messages (validate may fail if the token is incorrect)
  On Error Resume Next
  Err.Clear

  ' Ask the document to validate the license token
  docObj.Validate(licenseTokenInt)
  If Err Then
	  ValidateServer = 0    
  Else
	  ValidateServer = 1
  End If

End Function
